package com.ie.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.entity.CartEntity;
import com.ie.entity.GoodsEntity;
import com.ie.entity.OrderdetailEntity;
import com.ie.entity.OrdersEntity;
import com.ie.mapper.admin.OrderMapper;
import com.ie.service.before.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrdersEntity> implements OrderService {
    @Autowired
    private OrderMapper ordersMapper;
    @Autowired
    private OrderdetailService orderdetailService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CartService cartService;

    @Override
    public ResponseResult<Map<String, Object>> getAllOrders(OrdersEntity ordersEntity) {
        if (ordersEntity.getCurrentPage() == null || ordersEntity.getCurrentPage() < 1) ordersEntity.setCurrentPage(1);

        IPage<OrdersEntity> iPage = new Page<>(ordersEntity.getCurrentPage(), 5);
        //构造条件,用&& ||来组合更多的条件
        LambdaQueryWrapper<OrdersEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (ordersEntity.getId() != null) {
            lambdaQueryWrapper.eq(OrdersEntity::getId, ordersEntity.getId());
        }

        //分页查询
        IPage<OrdersEntity> page = page(iPage, lambdaQueryWrapper);
        Map<String, Object> myres = new HashMap<>();
        myres.put("allOrders", page.getRecords());
        myres.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<List<Map<String, Object>>> getOrdersDetail(OrdersEntity ordersEntity) {
        return ResponseResult.getSuccessResult(ordersMapper.getOrdersDetail(ordersEntity));
        //return ResponseResult.getSuccessResult(this.list());
    }

    @Override
    public ResponseResult<OrdersEntity> submitOrder(OrdersEntity ordersEntity) {
        ordersEntity.setStatus(0);
        ordersEntity.setOrderdate(new Date());
        this.save(ordersEntity);
        //生成详情
        List<Integer> bgid = ordersEntity.getBgid();
        List<Integer> bshoppingnum = ordersEntity.getBshoppingnum();
        ArrayList<OrderdetailEntity> bods = new ArrayList<OrderdetailEntity>();
        ArrayList<GoodsEntity> ges = new ArrayList<>();
        for (int i = 0; i < bgid.size(); i++) {
            OrderdetailEntity ort = new OrderdetailEntity();
            GoodsEntity ge = new GoodsEntity();
            ort.setOrderbasetableId(ordersEntity.getId());
            ort.setGoodstableId(bgid.get(i));
            ort.setShoppingnum(bshoppingnum.get(i));
            bods.add(ort);
            ge.setId(bgid.get(i));
            ge.setGstore(goodsService.getById(bgid.get(i)).getGstore() - bshoppingnum.get(i));
            ges.add(ge);
        }
        orderdetailService.saveBatch(bods);
        //清空购物车
        CartEntity cartEntity = new CartEntity();
        cartEntity.setBusertableId(ordersEntity.getBusertableId());
        cartService.clearCart(cartEntity);
        return ResponseResult.getSuccessResult(ordersEntity);
    }

    @Override
    public ResponseResult<OrdersEntity> goSubmitOrder(OrdersEntity ordersEntity) {
        ordersEntity.setStatus(0);
        ordersEntity.setOrderdate(new Date());
        this.save(ordersEntity);
        //生成详情
        List<Integer> bgid = ordersEntity.getBgid();
        List<Integer> bshoppingnum = ordersEntity.getBshoppingnum();
        ArrayList<OrderdetailEntity> bods = new ArrayList<OrderdetailEntity>();
        ArrayList<GoodsEntity> ges = new ArrayList<>();
        for (int i = 0; i < bgid.size(); i++) {
            OrderdetailEntity ort = new OrderdetailEntity();
            GoodsEntity ge = new GoodsEntity();
            ort.setOrderbasetableId(ordersEntity.getId());
            ort.setGoodstableId(bgid.get(i));
            ort.setShoppingnum(bshoppingnum.get(i));
            bods.add(ort);
            ge.setId(bgid.get(i));
            ge.setGstore(goodsService.getById(bgid.get(i)).getGstore() - bshoppingnum.get(i));
            ges.add(ge);
        }
        orderdetailService.saveBatch(bods);
        //清空购物车
        //CartEntity cartEntity = new CartEntity();
        //cartEntity.setBusertableId(ordersEntity.getBusertableId());
        //cartService.clearCart(cartEntity);
        return ResponseResult.getSuccessResult(ordersEntity);

    }

    @Override
    public ResponseResult<Map<String, Object>> getOrdersByUid(OrdersEntity ordersEntity) {
        Page<OrdersEntity> ipage = new Page<>(ordersEntity.getCurrentPage(), 5);
        QueryWrapper<OrdersEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("busertable_id", ordersEntity.getBusertableId());
        Page<OrdersEntity> page = page(ipage, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("ordersByUid", page.getRecords());
        map.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(map);
    }
}
